package com.skyolder.milkbee.web.rest;

import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.codahale.metrics.annotation.Timed;
import com.mysql.fabric.xmlrpc.base.Array;
import com.skyolder.milkbee.domain.Card;
import com.skyolder.milkbee.domain.Leaguer;
import com.skyolder.milkbee.dto.MonthGroupResult;
import com.skyolder.milkbee.security.AuthoritiesConstants;
import com.skyolder.milkbee.service.CardService;
import com.skyolder.milkbee.service.LeaguerService;
import com.skyolder.milkbee.service.UserService;
import com.skyolder.milkbee.tool.StringUtil;

import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/api/")
public class MonitorResource {
	@Inject
    private CardService cardService;
	@Inject
    private UserService userService;
    @Inject
    private LeaguerService leaguerService;
    
	@GetMapping("/groupmonth")
    @Timed
    @Secured(AuthoritiesConstants.ADMIN)
    public ResponseEntity<List<MonthGroupResult>> groupMonth(@ApiParam(required = false) String startDate){
		List<Leaguer> leagures=leaguerService.findByMonitorTrue();
		List<Long> leids=new ArrayList<Long>();
		for(Leaguer le:leagures){
			leids.add(le.getId());
		}
		ZonedDateTime month=null;
		if(StringUtil.isBlank(startDate)){
			month=ZonedDateTime.now();
		}else{
			month=ZonedDateTime.parse(startDate);
		}
		ZonedDateTime beginer=month.withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
		ZonedDateTime ender=beginer.plusMonths(1);
		ender=ender.minusNanos(1);
		List<MonthGroupResult> lists=cardService.doGroupMonth(leids,beginer,ender);
		return new ResponseEntity<>(lists,HttpStatus.OK);
	}
	@GetMapping("/daycount")
	@Timed
	@Secured(AuthoritiesConstants.ADMIN)
	public ResponseEntity<String> dayCount(){
		return new ResponseEntity("hello",HttpStatus.OK);
	}
}
